#include "user_extern.h"
#include "weatherlib.h"
void oled_show_weather(uint8_t x, uint8_t y, uint8_t code);
void oled_show_icon(uint8_t x, uint8_t y, uint8_t code);
void oled_show_sys_progress(uint8_t x, uint8_t y, int current, int max);
void oled_show_digimon(uint8_t x, uint8_t y, const char (*digimon)[6]);
void oled_show_sys_bmp(uint8_t x, uint8_t y, uint8_t index);
void oled_show_sys_aixin(uint8_t x, uint8_t y, uint8_t type);
void oled_show_sys_title(const char *title);
void oled_show_shirt(uint8_t x, uint8_t y, uint8_t type);
void oled_show_ill(uint8_t x, uint8_t y);

UI ui = {
    .draw_progress = oled_show_sys_progress,
    .draw_weather = oled_show_weather,
    .draw_sysicon = oled_show_icon,
    .draw_digimon = oled_show_digimon,
    .draw_sys_bmp = oled_show_sys_bmp,
    .draw_sys_xin = oled_show_sys_aixin,
    .draw_sys_title = oled_show_sys_title,
    .draw_digi_shirt = oled_show_shirt,
    .draw_digi_ill = oled_show_ill,
};

static int start_count(int cur, int max, int div)
{
    return (cur * div / max);
}
void oled_show_icon(uint8_t x, uint8_t y, uint8_t code)
{
    uint8_t i, n;
    for (i = 0; i < 5; i++)
    {
        for (n = 0; n < 40; n++)
            //OLED_GRAM[n+x][7-i + y] = day_sunny[n][i];
            OLED_GRAM[n + x][7 - i + y] = system_icon[code][n][i];
    }
}

void oled_show_weather(uint8_t x, uint8_t y, uint8_t code)
{
    uint8_t i, n;
    for (i = 0; i < 4; i++)
    {
        for (n = 0; n < 44; n++)
            //OLED_GRAM[n+x][7-i + y] = day_sunny[n][i];
            switch (code) //天气代码
            {
            case 0: //sunny
                OLED_GRAM[n + x][7 - i + y] = day_sunny[n][i];
                break;
            case 1: //clear
                OLED_GRAM[n + x][7 - i + y] = clear[n][i];
                break;
            case 4: //cloudy
                OLED_GRAM[n + x][7 - i + y] = cloudy[n][i];
                break;
            }
    }
}

/* 显示进度条 */
void oled_show_sys_progress(uint8_t x, uint8_t y, int current, int max)
{
    uint8_t i = 0, n = 0;
    //oled.show_string(x,y,(const uint8_t *)"xx",32,1);
    oled.draw_bmp(x, y, 4, 127, BMP_COVER_MODE, (void *)digi_sys_evolution_array); /* 显示经验条框架 */

    n = start_count(current, max, 16);
    while (i < n)
    {
        oled.draw_bmp(((i * 2) * 4 - 2), 2, 1, 4, BMP_COVER_MODE, (void *)digi_sys_evo_one); /* 显示经验点 */
        i++;
    }
}

/* OLED 应用层接口 */
/* 显示数码宝贝 */
void oled_show_digimon(uint8_t x, uint8_t y, const char (*digimon)[6])
{
    oled.draw_bmp(x, y, 6, 48, BMP_COVER_MODE, (void *)digimon);
}

/* 显示系统图标 */
void oled_show_sys_bmp(uint8_t x, uint8_t y, uint8_t index)
{
    oled.draw_bmp(x, y, 2, 16, BMP_COVER_MODE, digi_sys_array[index]);
}

/* 显示爱心 */
void oled_show_sys_aixin(uint8_t x, uint8_t y, uint8_t type)
{
    oled.draw_bmp(x, y, 4, 32, BMP_MIX_MODE, (void *)digi_sys_aixin_array[type]); /* type-0空心; type-1实心 */
}

void oled_show_sys_title(const char *title)
{
    oled.show_string(1, 0, T(const_setting_back), STR_SIZE_12, 0);
    oled.show_string(POSITION_CENTER(title, STR_SIZE_12), 0, T(title), STR_SIZE_12, 0);
}

void oled_show_shirt(uint8_t x, uint8_t y, uint8_t type)
{
    if (type)
    {
        oled.draw_bmp(x, y, 4, 32, BMP_COVER_MODE, (void *)digi_sys_stat_shirt[0]);
    }
    else
    {
        oled.draw_bmp_reversal(x, y, 4, 32, BMP_COVER_MODE, (void *)digi_sys_stat_shirt[0]);
    }
}

void oled_show_ill(uint8_t x, uint8_t y)
{
    oled.draw_bmp(x, y, 4, 32, BMP_COVER_MODE, (void *)digi_sys_stat_ill[0]);
}
